package com.fzrskj.cloud.free.sys.service;

import com.fzrskj.cloud.free.consts.CConsts;
import com.fzrskj.cloud.free.core.tool.CJsonTool;
import com.fzrskj.cloud.free.core.tool.CStrTool;
import com.fzrskj.cloud.free.sys.bean.SysArticle;
import com.fzrskj.cloud.free.sys.bean.SysArticleView;
import com.fzrskj.cloud.free.sys.utils.SysArticleUtils;
import ms.core.consts.SysErr;
import ms.core.object.CObject;
import ms.core.tool.XmlCodeParser;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SysArticleService extends CObject {
	
	/**
	 * 获取文章数据
	 * @param req
	 * @param resp
	 * @param p={"id":"id"}
	 * @param sid
	 * @return
	 */
	public String getSysArticle(HttpServletRequest req, HttpServletResponse resp, String p, String sid){
		int id = CJsonTool.getIntValue(p, "id", -1);
		if (id < 0 ) {
		return XmlCodeParser.formatResultTips(false, SysErr.SYS_PARAM_ERR);
		}
		SysArticleUtils utils = new SysArticleUtils();
		//获取数据
		SysArticle sa = utils.getsysArticle(id);
		String code = utils.getErrCode();
		//返回错误代码
		if (!code.equals(SysErr.CODE_SUCCESS)) {
			return XmlCodeParser.formatResultTips(false, code);
		}
		if(sa==null){
			return XmlCodeParser.formatResultTips(false, SysErr.SYS_DATA_NOT_FOUND);
		}
		String data =  CJsonTool.beanToJson(sa);

		String returnString = XmlCodeParser.formatResultData(true, SysErr.CODE_SUCCESS, data);

		//获取前后文章
		String recent = CJsonTool.getStrValue(p,"recent", "");
		if ("y".equals(recent.toLowerCase())) {
			String parentId = CJsonTool.getStrValue(p, "parentId", null);
			String typeId = CJsonTool.getStrValue(p, "typeId", null);

			//获取前一条
			SysArticleView last = utils.getRecent(id, 1, parentId, typeId);

			//后一条
			SysArticleView next = utils.getRecent(id, 2, parentId, typeId);

			returnString = CJsonTool.addNode(returnString, "next", null == last ? new HashMap<>() : last);
			returnString = CJsonTool.addNode(returnString, "last", null == next ? new HashMap<>() : next);
		}

		return returnString;
	}

	/**
	 * 根据标记获取文章
	 * @param req
	 * @param resp
	 * @param p={"flag":"文章标记"}
	 * @param sid
	 * @return
	 */
	public String getSysAgreement(HttpServletRequest req, HttpServletResponse resp, String p, String sid){
		String flag = CJsonTool.getStrValue(p, "flag", null);
		if (CStrTool.isBlankStr(flag) ) {
			return XmlCodeParser.formatResultTips(false, SysErr.SYS_PARAM_ERR);
		}
		SysArticleUtils utils = new SysArticleUtils();
		//获取数据
		SysArticle sa = utils.getsysArticleFlag(flag);
		String code = utils.getErrCode();
		//返回错误代码
		if (!code.equals(SysErr.CODE_SUCCESS))
			return XmlCodeParser.formatResultTips(false, code);
		String data =  CJsonTool.beanToJson(sa);
		return XmlCodeParser.formatResultData(true, SysErr.CODE_SUCCESS, data);
	}

	/**
	 * 修改文章
	 * @param req
	 * @param resp
	 * @param p={"id":表id,"title":"标题","content":"内容","attachmentUrl":"附件地址","parentId":"父类型ID","typeId":"子类型ID","flag":"协议标识"}
	 * @param sid
	 * @return
	 */
	public String modSysArticle(HttpServletRequest req, HttpServletResponse resp, String p, String sid){
		int id = CJsonTool.getIntValue(p, "id", -1);
		String title = CJsonTool.getStrValue(p, "title", null);
		String content = CJsonTool.getStrValue(p, "content", null);
		String parentId = CJsonTool.getStrValue(p, "parentId", null);
		String typeId = CJsonTool.getStrValue(p, "typeId", null);
		String flag = CJsonTool.getStrValue(p, "flag", null);
		if (id < 0 || CStrTool.isBlankStr(title) || CStrTool.isBlankStr(parentId) || CStrTool.isBlankStr(content)  ) {
			return XmlCodeParser.formatResultTips(false, SysErr.SYS_PARAM_ERR);
		}

		Map<String, Object> cdt = new HashMap<String, Object>();
		cdt.put("id", id);
		cdt.put("title", title);
		cdt.put("content", content);
		cdt.put("parentId", parentId);
		cdt.put("typeId", typeId);
		if (!CStrTool.isBlankStr(flag)) {cdt.put("flag", flag);}

		SysArticleUtils saUtils = new SysArticleUtils();
		int ret = saUtils.modSysArticle(cdt,sid);
		boolean success = (ret > 0);
		String code = saUtils.getErrCode();
		return XmlCodeParser.formatResultTips(success, code);
	}

	/**
	 * 删除文章
	 * @param req
	 * @param resp
	 * @param p={"id":"id"}
	 * @param sid
	 * @return
	 */
	public String delSysArticle(HttpServletRequest req, HttpServletResponse resp, String p, String sid){
		int id = CJsonTool.getIntValue(p, "id", -1);
		if (id < 0 ) {
			return XmlCodeParser.formatResultTips(false, SysErr.SYS_PARAM_ERR);
		}

		SysArticleUtils utils = new SysArticleUtils();

		if (utils.delSysArticle(id) > 0) {
			return XmlCodeParser.formatResultTips(true, SysErr.CODE_SUCCESS);
		} else {
			return XmlCodeParser.formatResultTips(false,SysErr.SYS_DATA_NOT_FOUND);
		}
	}

	/**
	 * 添加文章
	 * @param req
	 * @param resp
	 * @param p={""}
	 * @param sid
	 * @return
	 */
	public String addSysArticle(HttpServletRequest req, HttpServletResponse resp, String p, String sid){
		String title = CJsonTool.getStrValue(p, "title", null);
		String content = CJsonTool.getStrValue(p, "content", null);
		String attachmentUrl = CJsonTool.getStrValue(p, "attachmentUrl", null);
		String parentId = CJsonTool.getStrValue(p, "parentId", null);
		String typeId = CJsonTool.getStrValue(p, "typeId", null);
		String flag = CJsonTool.getStrValue(p, "flag", null);

		if (CStrTool.isBlankStr(title) || CStrTool.isBlankStr(parentId) || CStrTool.isBlankStr(content) ) {
			return XmlCodeParser.formatResultTips(false, SysErr.SYS_PARAM_ERR);
		}

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("title", title);
		map.put("content", content);
		map.put("attachmentUrl", attachmentUrl);
		map.put("parentId", parentId);
		map.put("typeId", typeId);
		map.put("flag", flag);

		SysArticleUtils saUtils = new SysArticleUtils();
		int ret = saUtils.addSysArticle(map,sid);
		boolean success = (ret > 0);
		String code = saUtils.getErrCode();
		return XmlCodeParser.formatResultTips(success, code);
	}

	/**
	 * 查询文章管理列表
	 * @param req
	 * @param resp
	 * @param p={"content":"搜索内容","parentId":"父类型ID","typeId":"子类型ID","flag":"协议标识","page":"页数","pagesize":"每页条数"}
	 * @param sid
	 * @return
	 */
	public String querySysArticle(HttpServletRequest req, HttpServletResponse resp, String p, String sid){
		String content = CJsonTool.getStrValue(p, "content", null);
		String parentId = CJsonTool.getStrValue(p, "parentId", null);
		String typeId = CJsonTool.getStrValue(p, "typeId", null);
		String flag = CJsonTool.getStrValue(p, "flag", null);
		int page = CJsonTool.getIntValue(p, "page", 1);
		int pagesize = CJsonTool.getIntValue(p, "pagesize", CConsts.PAGE_SIZE);

		Map<String, Object> map = new HashMap<String, Object>();
		if (!CStrTool.isBlankStr(content)) {map.put("content", content);}
		if (!CStrTool.isBlankStr(parentId)) {map.put("parentId", parentId);}
		if (!CStrTool.isBlankStr(typeId)) {map.put("typeId", typeId);}
		if (!CStrTool.isBlankStr(flag)) {map.put("flag", flag);}

		SysArticleUtils saUtils = new SysArticleUtils();
		int rows = saUtils.querySysArticle(map);
		String code = saUtils.getErrCode();
		if (!code.equals(SysErr.CODE_SUCCESS)) {
			return XmlCodeParser.formatResultTips(false, code);
		}

		int pages = Math.round((rows + pagesize - 1) / pagesize);
		List<SysArticleView> list = saUtils.querySysArticleList(map, page, pagesize);
		code = saUtils.getErrCode();

		if (!code.equals(SysErr.CODE_SUCCESS)) {
			return XmlCodeParser.formatResultTips(false, code);
		}
		//JSON数据
		String data = CJsonTool.listBeanToJson(list);
		return XmlCodeParser.formatResultPageData(code, page, pages, pagesize, rows, data);
	}

	/**
	 * 查询文章
	 * @param req
	 * @param resp
	 * @param p={"title":"标题","content":"内容","parentId":"父类型ID","typeId":"子类型ID","flag":"协议标识"}
	 * @param sid
	 * @return
	 */
	public String querySysArticleList(HttpServletRequest req, HttpServletResponse resp, String p, String sid){
		String title = CJsonTool.getStrValue(p, "title", null);
		String content = CJsonTool.getStrValue(p, "content", null);
		String parentId = CJsonTool.getStrValue(p, "parentId", null);
		String typeId = CJsonTool.getStrValue(p, "typeId", null);
		String flag = CJsonTool.getStrValue(p, "flag", null);
		if (CStrTool.isBlankStr(parentId)  ) {
		   return XmlCodeParser.formatResultTips(false, SysErr.SYS_PARAM_ERR);
		}
		Map<String, Object> map = new HashMap<String, Object>();
		if (!CStrTool.isBlankStr(title)) {map.put("title", title);}
		if (!CStrTool.isBlankStr(content)) {map.put("content", content);}
		if (!CStrTool.isBlankStr(parentId)) {map.put("parentId", parentId);}
		if (!CStrTool.isBlankStr(typeId)) {map.put("typeId", typeId);}
		if (!CStrTool.isBlankStr(flag)) {map.put("flag", flag);}

		SysArticleUtils saUtils = new SysArticleUtils();
		List<SysArticle> list = saUtils.querySysArticleList(map);
		String code = saUtils.getErrCode();
		if (!code.equals(SysErr.CODE_SUCCESS)) {
			return XmlCodeParser.formatResultTips(false, code);
		}
		//JSON数据
		String data = CJsonTool.listBeanToJson(list);
		return XmlCodeParser.formatResultData(true, code, data);
	}

	/**
	 * 查询文章管理列表
	 * @param req
	 * @param resp
	 * @param p={"flag":"协议标识"}
	 * @param sid
	 * @return
	 */
	public String querySysArticleFlag(HttpServletRequest req, HttpServletResponse resp, String p, String sid){

		String flag = CJsonTool.getStrValue(p, "flag", null);
		if (CStrTool.isBlankStr(flag)) {
			return XmlCodeParser.formatResultTips(false, SysErr.SYS_PARAM_ERR);
		}
		Map<String, Object> map = new HashMap<String, Object>();
		if (!CStrTool.isBlankStr(flag)) {map.put("flag", flag);}
		
		SysArticleUtils saUtils = new SysArticleUtils();
		int rows = saUtils.querySysArticle(map);
		String code = saUtils.getErrCode();
		if (rows > 0) {
			return XmlCodeParser.formatResultTips(true,code);
		} else {
			return XmlCodeParser.formatResultTips(false,code);
		}
	}
}
